<?php
import("@.ORG.Base");
class Heimingdan2Action extends Base{

    public function index() {
        if ($_REQUEST['ajax'] == 1) {
            $model = M("heimingdan2_info");
            $seek = isset($_POST['start']) ? $_POST['start'] : 0;

            if (isset($_POST['search'])) {
                $temp = explode('&', $_POST['search']['value']);
                $arr = array();
                if (is_array($temp)) {
                    foreach ($temp as $val) {
                        $val = explode('=', $val);
                        if (!empty($val[0]) && !empty($val[1])) $args = array();
                        $arr[$val[0]] = $val[1];
                    }
                }
            }
            $where = array();

            $postData = $_POST;
            $orderArr = array();
            foreach($postData['order'] as $val){
                $orderArr[] = $postData['columns'][$val['column']]['data'] .' '. $val['dir'];
            }
            $order = !empty($orderArr) ? implode(',',$orderArr) : ' add_time DESC ';
            $data = array();
            $data = $model->where($where)->limit("{$seek},20")->order($order)->select();
            foreach($data as &$val){
                $val['add_time'] = date('Y-m-d H:i',$val['add_time']);
                unset($val);
            }
            $count = $model->where($where)->count();
            $ret = array(
                'iTotalRecords' => $count,
                'iTotalDisplayRecords' => $count,
                'aaData' => (array)$data,
            );

            echo json_encode($ret);
            exit();
        }
        $model = M('daolu_info');
        $daoluData = $model->select();
        $this->assign('daoluData',$daoluData);
        Layout("adminLayout");
        $this->assign('title','常用语');
        $this->display();
    }

    public function detail() {
        C('LAYOUT_ON',false);
        $id = isset($_GET['id']) ? $_GET['id'] : 0;
        if( $id ){
            $model = M("heimingdan2_info");
            $data = $model->where(array('tag'=>1,'id'=>$id))->find();
            $data['addTime'] = date('Y-m-d H:i');
            $this->assign('data',$data);
            $this->assign('title','道路');
            $this->display();
        }
        else{
            $this->redirect('/Heimingdan2/index');
        }
    }

    public function add(){
        $model = M('daolu_info');
        $daoluData = $model->select();
        $this->assign('daoluData',$daoluData);
        $this->display();
    }

    public function addAjax(){
        if( !empty($_POST) ){
            $row = array(
                'add_time'   => SYS_TIME,
                'danwei_name'       => $_POST['danwei_name'],
                'fuzheren_name'     => $_POST['fuzheren_name'],
                'fuzheren_tel'      => $_POST['fuzheren_tel'],
                'point'             => $_POST['point'],
                'reason'            => $_POST['reason'],
            );
            $model = M('heimingdan2_info');
            $lastID = $model->add($row);
            if( $lastID ){
                $this->returnAjax(0,'操作成功');
            }
            $this->returnAjax(1002,'发生错误，操作失败');
        }
        $this->returnAjax(1001,'发生错误，操作失败');
    }

    public function update(){
        $id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
        if( $id ){
            $model = M('heimingdan2_info');
            $data = $model->where(array('id'=>$id))->find();
            $data['add_time'] = date('Y-m-d H:i');
            $this->assign("data",$data);
            $this->display();
        }
        else{
            $this->redirect('/Heimingdan2index');
        }
    }
    public function updateAjax(){
        $id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
        if( $id && !empty($_POST) ){
            $model = M('heimingdan2_info');
            $data['name'] = $_POST['name'];
            $data['reason'] = $_POST['reason'];
            $data['add_time'] = SYS_TIME;
            $model->where(array('id'=>$id))->data($data)->save(); // 根据条件保存修改的数据
            $this->returnAjax(0,'操作成功');
        }
    }

    public function del(){
        $id = isset($_REQUEST['id']) ? (int)$_REQUEST['id'] : 0;
        if( $id ){
            $model = M('heimingdan2_info');
            if($model->where("id={$id}")->delete()){
                $this->returnAjax(0);
            }
            else{
                $this->returnAjax(-1,'error');
            }
        }
    }

    public function checkAjax(){
        if( !empty($_POST) ){
            $name = isset($_POST['danwei_name']) ? $_POST['danwei_name'] : '';
            if($name){
                $model = M('heimingdan2_info');
                $data = $model->where(array('danwei_name'=>$name))->find();
                if($data){
                    $this->returnAjax(1000,$data['reason']);
                }
            }
        }
        $this->returnAjax(0,'');
    }
}
